package cn.edu.nju.software.android.activity;

import cn.edu.nju.software.android.util.AsyncImageLoader;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class PictureActivity extends Activity {
	private int screen_width;
	private int screen_height;
	private Handler failureHandler = new Handler(){
		public void handleMessage(Message msg) {
			Toast.makeText(PictureActivity.this, "WIFI没有打开", Toast.LENGTH_SHORT).show();
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		screen_width = size.x;
		screen_height = size.y;
		
		setContentView(R.layout.activity_picture);
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		ImageButton picture = null;
		picture = (ImageButton) findViewById(R.id.picture);
		try{
			picture.setOnTouchListener(new OnTouchListener(){     
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if(event.getAction() == MotionEvent.ACTION_UP){
						finish();
					}
					return false;
				}
			});
		}catch (Exception e) {
			e.printStackTrace();
		}
		String url = getIntent().getExtras().getString("picture");
		AsyncImageLoader imageAsyLoader = new AsyncImageLoader();
		Drawable cachedImage = imageAsyLoader.loadDrawable(this, failureHandler, url, picture,
				new AsyncImageLoader.ImageCallback() {
			@SuppressLint("NewApi")
			@Override
			public void imageLoaded(Drawable imageDrawable,
					ImageView imageView, String imageUrl) {
				if(imageDrawable != null)
					imageView.setImageDrawable(imageDrawable);
			}
		}, screen_width, screen_height);
		if (cachedImage == null) {
			picture.setImageResource(R.drawable.loading);
		} else {
			picture.setImageDrawable(cachedImage);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

}
